    <!DOCTYPE html>
<html>
<head>
    <meta name="layout" content="main" />
    <g:set var="entityName" value="${message(code: 'account.label', default: 'Account')}" />
    <title>机构管理</title>
    <asset:stylesheet src="ztree/demo.css"/>
    <asset:stylesheet src="ztree/zTreeStyle.css/"/>
    <asset:javascript src="ztree/jquery.ztree.core.js/"/>
    <asset:javascript src="ztree/jquery.ztree.excheck.js/"/>
    <asset:javascript src="ztree/jquery.ztree.exedit.js/"/>

    <asset:stylesheet src="styles/css/menu.css"/>

    %{--map libs--}%
    <asset:javascript src="newMap/libs/ol/ol.js" type="text/javascript" />
    <g:link href="../../assets/javascripts/newMap/libs/ol/ol.css" rel="stylesheet" type="text/css" />
    <asset:javascript src="newMap/libs/ol/xdmap.js" type="text/javascript" />
    <g:link href="../../assets/javascripts/newMap/libs/ol/map.css" rel="stylesheet" type="text/css" />
    <style>
        .ztree li span.button.add {margin-left:12px; margin-right: -1px; background-position:-128px -200px; vertical-align:top; *vertical-align:middle}
        .alertmap {
            position: fixed;
            z-index: 9999;
            left: 0;
            top: 0;
            right: 0;
            bottom: 0;
            /*background: #ccc;*/
            display: none;
            /*width: 1920px;*/
            /*height: 768px;*/
        }
        .modalHeight{
            height:100px;
            padding-top:5%;
        }
    </style>
    <script type="text/javascript">

        $(function(){
            $("#searchAccount").click(function () {
                var accountName = $("#accountName").val();
                initAccount(accountName)
            })
        })

        var setting = {
            check: {
                enable: false
            },
            view: {
                addHoverDom: addHoverDom,
                removeHoverDom: removeHoverDom,
                selectedMulti: false
            },
            edit: {
                enable: true,
                showRemoveBtn: showRemoveBtn,
                showRenameBtn: true
            },
            data: {
                simpleData: {
                    enable: true
                }
            },
            callback: {
                beforeEditName: beforeEditName,
                beforeRemove: beforeRemove,
                beforeRename: beforeRename,
                onRemove: onRemove,
                onRename: onRename,
                beforeClick: beforeClick,
                onClick: onClick,
                onCollapse: zTreeOnCollapse,
                onExpand: zTreeOnExpand
            }
        };

        var setting1 = {
            check: {
                enable: true,
                chkboxType: { "Y": "", "N": ""}
            },
            data: {
                simpleData: {
                    enable: true
                }
            },
            callback: {
                beforeCheck: beforeCheck,
                onCheck: onCheck,
                onCollapse: zTreeOnCollapse1,
                onExpand: zTreeOnExpand1
            }
        };


        // 初始化数据
        initAccount();

        // 初始化数据

        function initAccount(name) {
            $.ajax({
                type: "post",
                url: "/account/getAllAccountList",
                data:{
                    name: name
                },
                success: function (data) {
                    if (data.status == "success") {
                        var accountList = data.accountList;
                        $(document).ready(function() {
                            $.fn.zTree.init($("#treeDemo"), setting, accountList);
                            expandAll("treeDemo");
                            var list = $('#treeDemo').children().children('ul');
                            list.css('paddingLeft','27px');
                            if(list.children().children('ul')){
                                list = list.children().children('ul');
                                list.css('paddingLeft','27px');
                                callbackUl(list);
                            }
                            function callbackUl (ul) {
                                if(ul.length != 0){
                                    ul = ul.children().children('ul');
                                    ul.css('paddingLeft', '27px');
                                    callbackUl(ul)
                                }else{
                                    return;
                                }
                            }

                            $.fn.zTree.init($("#treeDemo1"), setting1, accountList);
                            expandAll("treeDemo1");
                            var list1 = $('#treeDemo1').children().children('ul');
                            list1.css('paddingLeft','27px');
                            if(list1.children().children('ul')){
                                list1 = list1.children().children('ul');
                                list1.css('paddingLeft','27px');
                                callbackUl1(list1);
                            }
                            function callbackUl1 (li) {
                                if(li.length != 0){
                                    li = li.children().children('ul');
                                    li.css('paddingLeft', '27px');
                                    callbackUl(li)
                                }else{
                                    return;
                                }
                            }
                        });
                    }
                },
                error: function () {
                    console.log("获取失败，请稍后重试");
                },
            });
        }
        // 查询机构
        function queryAccountById(id) {
            var account;
            $.ajax({
                type: "post",
                url: "/account/queryAccountById",
                data:{
                    id: id
                },
                async: false,
                success: function (data) {
                    if (data.status == "success") {
                        account = data.account
                    }
                },
                error: function () {
                    console.log("获取失败，请稍后重试");
                },
            });
            return account;
        }

        // 查询机构
        function queryAllAccountManageById(id) {
            var accountManageList;
            $.ajax({
                type: "post",
                url: "/account/queryAllAccountManageById",
                data:{
                    id: id
                },
                async: false,
                success: function (data) {
                    if (data.status == "success") {
                        accountManageList = data.accountManageList
                    }
                },
                error: function () {
                    console.log("获取失败，请稍后重试");
                },
            });
            return accountManageList;
        }

        // 编辑机构
        function editAccount(id, name) {
            $.ajax({
                type: "post",
                url: "/account/editAccount",
                data:{
                    id: id,
                    name: name
                },
                success: function (data) {
                    if (data.status == "success") {
                        toastr.error('修改成功');
                        initAccount();
                    }
                },
                error: function () {
                    console.log("获取失败，请稍后重试");
                },
            });
        }
        // 新增机构
        function addAccount(pId) {
            var account;
            $.ajax({
                type: "post",
                url: "/account/addAccount",
                data:{
                    pId: pId
                },
                async: false,
                success: function (data) {
                    if (data.status == "success") {
                        account = data.account;
                    }
                },
                error: function () {
                    console.log("获取失败，请稍后重试");
                },
            });
            return account;
        }

        // 删除机构
        function deleteAccount(id) {
            $.ajax({
                type: "post",
                url: "/account/deleteAccount",
                data:{
                    id: id
                },
                success: function (data) {
                    if (data.status == "success") {
                        toastr.error('删除成功');
                        initAccount();
                    }
                },
                error: function () {
                    console.log("获取失败，请稍后重试");
                },
            });
        }

        // 机构权限
        function addAccountManageAccount(id, manageId, flag) {
            $.ajax({
                type: "post",
                url: "/account/addAccountManageAccount",
                data:{
                    id: id,
                    manageId: manageId,
                    flag: flag
                },
                success: function (data) {
                    if (data.status == "success") {
                        toastr.error('权限更新成功');
                        return
                    }
                },
                error: function () {
                    console.log("获取失败，请稍后重试");
                },
            });
        }


        var log, className = "dark";

        function expandAll(treeId) {
            var zTree = $.fn.zTree.getZTreeObj(treeId);
            zTree.expandAll(true);
            $('#treeDemo a span.ico_open').attr('class','button ico_open fa fa-folder-o');
            $('#treeDemo1 a span.ico_open').attr('class','button ico_open fa fa-folder-o');
        }

        function beforeCheck(treeId, treeNode) {
            var id = $("#clickedAccount").val();
            if (!id) {
                toastr.error('请先选择机构！');
                return false;
            }
            return (treeNode.doCheck !== false);
        }
        function onCheck(e, treeId, treeNode) {
            var id = $("#clickedAccount").val();
            var manageId = treeNode.id;
            var flag = treeNode.checked;
            addAccountManageAccount(id, manageId, flag);
        }
        function beforeEditName(treeId, treeNode) {
            className = (className === "dark" ? "":"dark");
            var zTree = $.fn.zTree.getZTreeObj("treeDemo");
            zTree.selectNode(treeNode);
            setTimeout(function() {
                var account = queryAccountById(treeNode.id);
                $("#id").val(treeNode.id);
                $("#name").val(treeNode.name);
                $("#address").val(account.address);
                var parent = account.parent
                if (parent) {
                    $("#parent").val(parent.id);
                    $("#s2id_parent span:first").html(parent.name);
                }
                var level = account.level
                if (level) {
                    $("#level").val(level.id);
                    $("#s2id_level span:first").html(level.name);
                }
                $("#phone").val(account.phone);

                var province = account.province
                if (province) {
                    $("#province").val(province.id);
                    getCityList(province.id)
                    $("#s2id_province span:first").html(province.name);
                }
                var city = account.city
                if (city) {
                    $("#city").val(city.id);
                    getDistrictList(city.id)
                    $("#s2id_city span:first").html(city.name);
                }
                var district = account.district
                if (district) {
                    $("#district").val(district.id);
                    $("#s2id_district span:first").html(district.name);
                }
                $("#contact").val(account.contact);
                $("#cellphone").val(account.cellphone);
                $("#x").val(account.x);
                $("#y").val(account.y);

                $("#title").text("编辑机构");

                $("#updateAccount").click();
            }, 0);
            return false;
        }

        function getCityList(province)
        {
            $.ajax({
                url: "/commonUtils/getAreaByParentId",
                type: "POST",
                data: {'parentId': province} ,
                success: function(result){
                    for(var i=0; i < result.list.length; i++){
                        $("#city").append("<option value=" + result.list[i].id + ">" + result.list[i].name + "</option>")
                    }
                }
            });
        }

        function getDistrictList(city)
        {
            $.ajax({
                url: "/commonUtils/getAreaByParentId",
                type: "POST",
                data: {'parentId': city} ,
                success: function(result){
                    for(var i=0; i < result.list.length; i++){
                        $("#district").append("<option value=" + result.list[i].id + ">" + result.list[i].name + "</option>")
                    }
                }
            });
        }

        function beforeClick(treeId, treeNode, clickFlag) {
            $("#clickedAccount").val(treeNode.id);
            return (treeNode.click != false);
        }
        function onClick(event, treeId, treeNode, clickFlag) {
            var accountManageList = queryAllAccountManageById(treeNode.id);
            $.fn.zTree.init($("#treeDemo1"), setting1, accountManageList);
            expandAll("treeDemo1");
            $('#treeDemo li a span.ico_docu').parent().parent().css('paddingLeft','27px');
            $('#treeDemo1 li a span.ico_docu').parent().parent().css('paddingLeft','27px');
                var list1 = $('#treeDemo1').children().children('ul');
                list1.css('paddingLeft','27px');
                if(list1.children().children('ul')){
                    list1 = list1.children().children('ul');
                    list1.css('paddingLeft','27px');
                    callbackUl1(list1);
                }
                function callbackUl1 (li) {
                    if(li.length != 0){
                        li = li.children().children('ul');
                        li.css('paddingLeft', '27px');
                        callbackUl1(li)
                    }else{
                        return;
                    }
                }


        }
        function zTreeOnCollapse() {
            $('#treeDemo a span.ico_close').attr('class','button ico_close fa fa-folder-open-o');
        };
        function zTreeOnExpand() {
            $('#treeDemo a span.ico_open').attr('class','button ico_open fa fa-folder-o');
        };
        function zTreeOnCollapse1() {
            $('#treeDemo1 a span.ico_close').attr('class','button ico_close fa fa-folder-open-o');
        };
        function zTreeOnExpand1() {
            $('#treeDemo1 a span.ico_open').attr('class','button ico_open fa fa-folder-o');
        };

        function beforeRemove(treeId, treeNode) {
            className = (className === "dark" ? "":"dark");
            var zTree = $.fn.zTree.getZTreeObj("treeDemo");
            zTree.selectNode(treeNode);
            return confirm("请确认是否删除 ”" + treeNode.name + "“ 机构？");
        }
        function onRemove(e, treeId, treeNode) {
            deleteAccount(treeNode.id);
        }
        function beforeRename(treeId, treeNode, newName, isCancel) {
            className = (className === "dark" ? "":"dark");
            if (newName.length == 0) {
                setTimeout(function() {
                    var zTree = $.fn.zTree.getZTreeObj("treeDemo");
                    zTree.cancelEditName();
                    toastr.error('机构名称不能为空！');
                }, 0);
                return false;
            }
            return true;
        }
        function onRename(e, treeId, treeNode, isCancel) {
            editAccount(treeNode.id, treeNode.name)
        }
        function showRemoveBtn(treeId, treeNode) {
            return !treeNode.children;
        }
        function showRenameBtn(treeId, treeNode) {
            return !treeNode.isLastNode;
        }

        var newCount = 1;
        function addHoverDom(treeId, treeNode) {
            var sObj = $("#" + treeNode.tId + "_span");
            if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0) return;
            var addStr = "<span class='button add' id='addBtn_" + treeNode.tId + "' title='' onfocus='this.blur();' style='position: relative;'><i class='fa fa-plus-square-o' style='font-size: 22px;margin-top: 2px;margin-left: 1px;'></i></span>";
            sObj.after(addStr);
            var btn = $("#addBtn_"+treeNode.tId);
            if (btn) btn.bind("click", function(){
                $("#parent").val(treeNode.id);
                $("#addAccount").click();
                // var account = addAccount(treeNode.id);
                // var zTree = $.fn.zTree.getZTreeObj("treeDemo");
                // zTree.addNodes(treeNode, {id: account.id, pId: account.parent.id, name: account.name});
                // initAccount();
                return false;
            });
            var addDivFlag;
            $('.add').hover(function(){
                if (!addDivFlag) {
                    var addDiv = $(this).append('<div class="add-div" style="position: absolute;top: 24px;height:20px; background: #39485C;text-align: center; line-height: 20px; border-radius: 4px; color: #fff; width: 40px; left: -10px;">添 加</div>');
                    addDivFlag === true;
                }else {
                    return
                }

            },function(){
                $(".add-div").hide();
            })
        };
        function removeHoverDom(treeId, treeNode) {
            $("#addBtn_"+treeNode.tId).unbind().remove();
        };



    </script>
</head>
<body>

<div class="small-header">
    <div class="hpanel">
        <div class="panel-body">
            <div id="hbreadcrumb" class="pull-right navFloatRight">
                <ol class="hbreadcrumb breadcrumb">
                    <li>CINDATA PLS</li>
                    <li class="active">
                        <span>银行机构管理</span>
                    </li>
                </ol>
            </div>

            <h2 class="font-light m-b-xs">
                银行机构管理
            </h2>
        </div>
    </div>
</div>

<div class="panel-body" style="display: flex; justify-content: center;">
    <div class="form-group col-sm-6">
        <div class="col-sm-7">
            <input type="text" class="form-control" id="accountName" name="accountName" placeholder="请输入机构名称">
        </div>
        <a class="btn btn-info" id="searchAccount">搜索</a>
        <a class="btn btn-primary hide" id="addAccount" style="margin-left: 10px">新增总行</a>
        <a class="btn btn-info hide" href="#myModal" data-toggle="modal" id="updateAccount">新增总行</a>
        <g:if test="${this?.accountImport}"><a class="btn btn-success" href="#myModal1" data-toggle="modal" id="importAccount" style="margin-left: 10px">机构导入</a></g:if>
    </div>
</div>

<input type="hidden" name="clickedAccount" id="clickedAccount">


%{--树形菜单--}%

<div class="content" style="padding-top: 0;">
    <div class="row">
        <div class="col-lg-6">
            <div class="hpanel">
                <div class="panel-body">
                    <ul id="treeDemo" class="ztree"></ul>
                </div>
            </div>
        </div>

        <div class="col-lg-6">
            <div class="hpanel">
                <div class="panel-body">
                    <ul id="treeDemo1" class="ztree"></ul>
                </div>
            </div>
        </div>
    </div>
</div>

%{--机构导入--}%
<div class="content animate-panel p-b-n">
    <div class="row">
        <div class="hpanel">
            <div class="modal fade hmodal-success" id="myModal1" tabindex="-1" role="dialog" aria-hidden="true">
                <div class="modal-dialog">
                    <g:form action="accountCsvImport" method="post" enctype="multipart/form-data" class="formDataLine">
                        <div class="modal-content">
                            <div class="modal-body modalHeight">
                                <div class="form-group">
                                    <div class="col-md-2">
                                        <a href='javascript:void(0);' class="blueButton" style="line-height: 34px;font-size: 12px;">选择文件</a>
                                        <input type="file" class="myFileUpload" id="myFile" name="myFile" accept=".csv"/>
                                    </div>
                                    <div class="col-md-10" style="padding-left:25px;">
                                        <div id="file_div" class="file_show" ></div>
                                    </div>
                                </div>
                            </div>

                            <div class="modal-footer">
                                <a class="pull-left" href="../assets/accountTemplate.zip" style="font-weight:bold;text-decoration:none;color:#fff;" download>
                                    <button type="button" class="btn btn-info">下载导入模板</button>
                                </a>
                                <button type="button" class="btn btn-primary start" id="import" name="import">
                                    <span>开始导入</span>
                                </button>
                                <button type="reset" class="btn btn-danger" data-dismiss="modal">
                                    <span>取消导入</span>
                                </button>
                            </div>
                        </div>
                    </g:form>
                </div>
            </div>
        </div>
    </div>
</div>

%{--添加机构弹窗--}%
<div class="content animate-panel p-b-n">
    <div class="row">
        <div class="hpanel">
            <div class="modal fade hmodal-info" id="myModal" tabindex="-1" role="dialog" aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="color-line"></div>
                        <div class="modal-header">
                            <h4 class="modal-title"><span id="title">添加总行</span><small style="font-size: 14px" class="font-bold"> (注：<span style="color: red">*</span> 为必填)</small></h4>
                        </div>
                        <g:form action="accountSave" class="form-horizontal" name="myForm">
                            <div class="modal-body form-horizontal">

                                <input type="hidden" name="id" id="id" value="">
                                <g:select class="hide" name="parent.id" id="parent" from="${com.cindata.Account.findAllByType(com.cindata.dictionary.AccountTypeDict.BANK())}" optionKey="id" optionValue="name" noSelection="${['': '请选择上级机构']}"/>

                                <div class="form-group">
                                    <label class="col-md-2 control-label is-required">机构名称</label>
                                    <div class="col-md-4">
                                        <g:textField class="form-control" type="text" name="name" id="name"/>
                                    </div>

                                    <label class="col-md-2 control-label is-required">机构地址</label>
                                    <div class="col-md-4">
                                        <g:textField class="form-control" type="text" name="address" id="address"/>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="col-md-2 control-label is-required">机构级别</label>
                                    <div class="col-md-4">
                                        <g:select class="form-control" name="level.id" id="level" from="${com.cindata.dictionary.AccountLevelDict.list()}" optionKey="id" optionValue="name" noSelection="${['': '请选择机构级别']}"/>
                                    </div>

                                    <label class="col-md-2 control-label">机构电话</label>
                                    <div class="col-md-4">
                                        <g:textField class="form-control" type="text" name="phone" id="phone"/>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="col-md-2 control-label is-required">所在省份</label>
                                    <div class="col-md-4">
                                        <g:select class="form-control" name="province.id" id="province" from="${com.cindata.Province.list()}" optionKey="id" optionValue="name" noSelection="${['': '请选择所在省份']}"/>
                                    </div>

                                    <label class="col-md-2 control-label is-required">所在城市</label>
                                    <div class="col-md-4">
                                        <g:select class="form-control" name="city.id" id="city" from="${com.cindata.City.list()}" optionKey="id" optionValue="name" noSelection="${['': '请选择所在城市']}"/>
                                    </div>
                                    
                                </div>

                                <div class="form-group">
                                    <label class="col-md-2 control-label is-required">所在区县</label>
                                    <div class="col-md-4">
                                        <g:select class="form-control" name="district.id" id="district" from="${com.cindata.District.list()}" optionKey="id" optionValue="name" noSelection="${['': '请选择所在区县']}"/>
                                    </div>

                                    <label class="col-md-2 control-label is-required">联系人</label>
                                    <div class="col-md-4">
                                        <g:textField class="form-control" type="text" name="contact" id="contact"/>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="col-md-2 control-label is-required">手机号码</label>
                                    <div class="col-md-4">
                                        <g:textField class="form-control" type="text" name="cellphone" id="cellphone"/>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="col-md-2 control-label is-required">经度</label>
                                    <div class="col-md-4">
                                        <g:textField class="form-control" type="text" name="x" id="x" readonly="true"/>
                                    </div>

                                    <label class="col-md-2 control-label is-required">纬度</label>
                                    <div class="col-md-4">
                                        <g:textField class="form-control" type="text" name="y" id="y" readonly="true"/>
                                    </div>
                                </div>
                            </div>

                            <div class="modal-footer">
                                <button type="button" class="btn btn-primary pull-left mapShow">地图选点</button>
                                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                                <button type="button" class="btn btn-primary" id="submit">确定</button>
                            </div>
                        </g:form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div id="map" class="alertmap"></div>

<g:javascript>
    
    //导入功能按钮执行
    $(function () {
        $('#treeDemo li a span.ico_docu').parent().parent().css('paddingLeft','27px');
        $('#treeDemo1 li a span.ico_docu').parent().parent().css('paddingLeft','27px');
        $("#import").click(function () {
            var myFile = $("#myFile").val();
            var ext_name = myFile.substr(myFile.lastIndexOf(".")+1);
            var file_ext_arr = [
                'csv'
            ];
            if (myFile == "") {
                $("#file_div").html("请选择文件");
                $("#file_div").css("color", "red");
            }
            
            if (myFile !== "" && file_ext_arr.indexOf(ext_name)>-1) {
                $(this).attr("type", "submit");
            }
        });

        $(".myFileUpload").change(function () {
            var arrs = $(this).val().split('\\');
            var filename = arrs[arrs.length - 1];
            $("#file_div").html(filename);
            $("#file_div").css("color", "#3498db");
        });
    })

    $(function() {

        if ('${params.status}' == 'failed')
        {
            toastr.error('机构导入失败，请检查导入文档是否有问题！');
            return;
        }

        $(".mapShow").click(function () {
            var type ,id;
            var province = $("#province").val();
            var city = $("#city").val();
            var distrint = $("#district").val();
            if (province != null) {
                if(province != '-1'){
                    type = 'province';
                    id = province
                }

            }
            if (city != null) {
                if(city != '-1'){
                    type = 'city';
                    id = city
                }
            }
            // if(distrint != null) {
            //     if(distrint != '-1'){
            //         type = 'distrint';
            //         id = distrint
            //     }
            // }
            var url, zoom;
            $.ajax({
                url: '/account/getZoneNumber',
                async: false,
                type: 'POST',
                data: {
                    id: id
                },
                success: function (data) {
                    debugger;
                    var zoneNumber = data.result[0].zone_number;
                    switch (type){
                        case 'province':{
                            zoom = 6;
                            zoneNumber = zoneNumber.slice(0,2);
                            url = '../../assets/newMap/data/mapdata/city/' + zoneNumber + '.json';
                        }break;
                        case 'city':{
                            zoom = 8;
                            zoneNumber = zoneNumber.slice(0,2);
                            url = '../../assets/newMap/data/mapdata/city/' + zoneNumber + '.json';
                        }break;
                        // case 'distrint':{
                        //     zoom = 10;
                        //     url = '../../assets/newMap/data/mapdata/county/' + zoneNumber + '.json';
                        // }break;
                    }
                    $.ajax({
                        url: url,
                        async: false,
                        type: 'GET',
                        success:function (data) {
                            $(".alertmap").show();
                            $("body").css("overflow", "scroll");
                            $("body").css("overflow-y", "hidden");
                            var center = ol.proj.transform(data.cp, 'EPSG:4326', 'EPSG:3857');
                            var map = xdmap.mapInit('map', zoom, center);
                            map.on('dblclick',function (e) {
                                var point = ol.proj.transform(e.coordinate,'EPSG:3857','EPSG:4326');
                                $("#x").val(point[0]);
                                $("#y").val(point[1]);
                                $(".alertmap").hide();
                            });
                        }
                    })
                }
            })

        })
        $('.ztree li a span.button').addClass('fa fa-folder-o');
        $('#treeDemo li span.ico_docu').attr('class','button ico_docu fa fa-file-o');
        $('#treeDemo1 li span.ico_docu').attr('class','button ico_docu fa fa-file-o');
        $("#addAccount").click(function() {
            $("#id").val("");
            $("#name").val("");
            $("#address").val("");
            $("#level").val("");
            $("#s2id_level span:first").html("请选择机构级别");
            $("#phone").val("");
            $("#province").val("");
            $("#s2id_province span:first").html("请选择所在省份");
            $("#city").val("");
            $("#s2id_city span:first").html("请选择所在城市");
            $("#district").val("");
            $("#s2id_district span:first").html("请选择所在区县");
            $("#contact").val("");
            $("#cellphone").val("");
            $("#x").val("");
            $("#y").val("");
            $("#title").text("添加机构");
            $("#updateAccount").click();
        });

        $("#submit").click(function () {
            var name = $("#name").val();
            if(!name)
            {
                toastr.error('请输入机构名称！');
                return;
            }

            var address = $("#address").val();
            if(!address)
            {
                toastr.error('请输入机构地址！');
                return;
            }

            var level = $("#level").val();
            if(!level)
            {
                toastr.error('请选择机构级别！');
                return;
            }

            var province = $("#province").val();
            if(!province || province == "-1")
            {
                toastr.error('请选择所在省份！');
                return;
            }

            var city = $("#city").val();
            if(!city || city == "-1")
            {
                toastr.error('请选择所在城市！');
                return;
            }

            var district = $("#district").val();
            if(!district || district == "-1")
            {
                toastr.error('请选择所在区县！');
                return;
            }

            var contact = $("#contact").val();
            if(!contact)
            {
                toastr.error('请输入联系人！');
                return;
            }

            var cellphone = $("#cellphone").val();
            if(!cellphone)
            {
                toastr.error('请输入手机号码！');
                return;
            }

            var x = $("#x").val();
            if(!x)
            {
                toastr.error('请输入经度！');
                return;
            }

            var y = $("#y").val();
            if(!y)
            {
                toastr.error('请输入纬度！');
                return;
            }

            $("#submit").attr("type", "submit");
        })
    })

    $(function() {

        //省份切换
        $("#province").bind("change", function() {
            $("#city option").remove();
            $("#s2id_city span:first").html("请选择所在城市");
            $("#city").append("<option value='-1' >请选择所在城市</option>");

            $("#district option").remove();
            $("#s2id_district span:first").html("请选择所在区县");
            $("#district").append("<option value='-1' >请选择所在区县</option>");

            var province = $("#province").val();
            if(!province) {
                return
            }
            $.ajax({
                url: "/commonUtils/getAreaByParentId",
                type: "POST",
                data: {'parentId': province} ,
                success: function(result){
                    for(var i=0; i < result.list.length; i++){
                        $("#city").append("<option value=" + result.list[i].id + ">" + result.list[i].name + "</option>")
                    }
                }
            });
        });

        //城市切换
        $("#city").bind("change", function() {
            $("#district option").remove();
            $("#s2id_district span:first").html("请选择所在区县");
            $("#district").append("<option value='-1' >请选择所在区县</option>");
            var city = $("#city").val();
            if(!city) {
                return
            }
            $.ajax({
                url: "/commonUtils/getAreaByParentId",
                type: "POST",
                data: {'parentId': city} ,
                success: function(result){
                    for(var i=0; i < result.list.length; i++){
                        $("#district").append("<option value=" + result.list[i].id + ">" + result.list[i].name + "</option>")
                    }
                }
            });
        });
    })

</g:javascript>
</body>
</html>

